<template>
  <view class="my-settle-container">
    <!-- 全选区域 -->
    <label class="radio" @click="toggle">
      <radio color="#c00000" :checked="isAllSelect" />全选
    </label>
    <!-- 合计-->
    <view class="amount-box"
      >合计：
      <text class="amount">￥{{ getSelectPrice }}</text>
    </view>
    <!-- 结算按钮 -->
    <view
      class="btn-settle"
      @click="settle"
      :style="{
        'background-color': getSelectTotal === 0 ? '#bdbdbd' : '#c00000'
      }"
      >结算({{ getSelectTotal }})</view
    >
  </view>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
export default {
  data () {
    return {
      time: 3,
      timer: null,
      isSettle:false,//是否正在进行结算
    }
  },
  computed: {
    ...mapGetters('cart', ['getSelectTotal', 'getSelectPrice', 'isAllSelect']),
    ...mapGetters('user', ['getAddress', 'getToken', 'getRouteInfo'])
  },
  methods: {
    ...mapMutations('cart', ['selectOrNotSelect']),
    ...mapMutations('user', ['setRouteInfo']),
    // 全选和反选
    toggle () {
      this.selectOrNotSelect(!this.isAllSelect)
    },
    //   结算事件
    settle () {
        if(this.isSettle){
            return
        }
        this.isSettle=true
      // 判断是否勾选了商品（提示）
      if (this.getSelectTotal === 0) {
        uni.showToast({
          title: '请至少选择一个商品进行结算~',
          icon: 'none'
        })
        return
      }
      // 判断是否选择了收货地址(提示)
      if (!this.getAddress) {
        uni.showToast({
          title: '请选择收货地址',
          icon: 'none'
        })
        return
      }
      // 判断是否登录(如果没有登录，则提示，然后跳转到我的页面)
      if (!this.getToken) {
        uni.showToast({
          title: `请登录后再结算！${this.time}秒后自动跳转到登录页`,
          icon: 'none'
        })
        this.timer = setInterval(() => {
            // 倒计时结束
          if (this.time <= 1) {
            clearInterval(this.timer)
            this.time = 3
            // 跳转到我的页面
            uni.switchTab({
              url: '/pages/my/index',
              success: () => {
                //   保存路由信息
                this.setRouteInfo('/pages/cart/index')
                this.isSettle=false
              }
            })
            return
          }
          this.time--
          uni.showToast({
            title: `请登录后再结算！${this.time}秒后自动跳转到登录页`,
            icon: 'none'
          })
        }, 1000)
        return
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.my-settle-container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  // 将背景色从 cyan 改为 white
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px;
  font-size: 14px;

  .radio {
    display: flex;
    align-items: center;
  }

  .amount {
    color: #c00000;
  }

  .btn-settle {
    height: 50px;
    min-width: 100px;
    background-color: #c00000;
    color: white;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
  }
}
</style>
